<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/7/11
 * Time: 11:32
 */

namespace app\command\company\data_change;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class WarehouseStorageGoodsLogs extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('data:change:2')->setDescription('出入库表中添加经销商ID/公司ID');
    }


    protected function execute(Input $input, Output $output)
    {
        $output->writeln('出入库表中添加经销商ID/公司ID');
        $model = new \app\company\model\WarehouseStorageGoodsLogs();
        $model->startTrans();
        try {
            $model->select()->each(function ($item) {
                $supplier_id = (int)$item->companyOrderStorage()->value('supplier_id');
                $company_id = (int)$item->companyOrderStorage()->value('company_id');
                $item->company_id = $company_id;
                $item->supplier_id = $supplier_id;
                $item->save();
            });
            $model->commit();
            $output->writeln('出入库表中添加经销商ID/公司ID成功');
        } catch (\Exception $e) {
            $model->rollback();
            $output->writeln('出入库表中添加经销商ID/公司ID失败' . $e->getMessage() . $e->getLine() . $e->getFile());
        }
    }
}